package cn.itrip.auth.service;

import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.dao.user.ItripUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/** 
* @Description: 用户实现
* @Author: XiaoKun
* @Date: 2019-01-26 
*/ 
@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private ItripUserMapper itripUserMapper;

    /**
     * 用户注册
     *
     * @param itripUser
     * @return
     */
    @Override
    public Integer userRegister(ItripUser itripUser) throws Exception {
        if (itripUserMapper.findUserByUserCode(itripUser.getUserCode()) != null) {
            return -1;
        }
        return itripUserMapper.insertItripUser(itripUser);
    }

    /**
     * 验证用户是否存在
     *
     * @param param
     * @return
     */
    @Override
    public Integer ckusrUser(Map param) throws Exception {
        return itripUserMapper.getItripUserCountByMap(param);
    }

    /**
     * 判断用户名密码
     *
     * @param userCode
     * @return
     */
    @Override
    public ItripUser login(String userCode, String userPassword) throws Exception {
        ItripUser itripUser = itripUserMapper.findUserByUserCode(userCode);
        if (null != itripUser && itripUser.getUserPassword().equals(userPassword)) {
            if (itripUser.getActivated() != 1) {
                throw new Exception("用户未激活");
            }
            return itripUser;
        }
        return null;
    }

    /**
     * 激活用户
     *
     * @param userCode
     * @return
     * @throws Exception
     */
    @Override
    public Integer userActivate(String userCode) throws Exception {
        ItripUser user = itripUserMapper.findUserByUserCode(userCode);
        if (user == null) {
            throw new Exception("用户不存在！");
        }
        user.setActivated(1);
        return itripUserMapper.updateItripUser(user);
    }


}
